package fr.utt.lo02.CrazyEights.model.strategies;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Random;

import fr.utt.lo02.CrazyEights.model.Game;
import fr.utt.lo02.CrazyEights.model.cards.Card;
import fr.utt.lo02.CrazyEights.model.cards.CardSuit;
import fr.utt.lo02.CrazyEights.model.players.Player;



/**
 * This strategy just does *everything* randomly !
 */
public class StrategyRandom implements Strategy,Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * We choose a random Suit among the cards
	 * the player possess.
	 */
	public CardSuit chooseSuit(Player player) {
		ArrayList<CardSuit> availableSuits = new ArrayList<CardSuit>();
		Random r = new Random();
		int i = 0;
		int rand;
		
			// We build a list of Suit that the Player has in hand
			for(Card c : player.getCardsInHand())
				if(!availableSuits.contains(c.getSuit())) // avoiding redundancy
						availableSuits.add(c.getSuit());
		
		// Let's pick one randomly
		rand = r.nextInt(availableSuits.size());
			for(CardSuit s : availableSuits)
				if(i == rand)
					return s;
				else
					i++;

		return null;
	}

	/**
	 * Here we just choose a random Cards among the cards
	 * the player possess.
	 */
	public Card play(Player player) {
		Random r = new Random();
		ArrayList<Card> tHand = new ArrayList<Card>();
		Card playCard = null;
		int rand;
		
			// We create a temporary hand
			for(Card d : player.getCardsInHand())
				tHand.add(d);

			// We choose one random Card and try to play w/ it
			// if we can't, we delete it and try another one
			while(!tHand.isEmpty()) {
				rand = r.nextInt(tHand.size());
				playCard = tHand.get(rand);
				tHand.remove(rand);
				
					if(Game.getInstance().getDiscardPile().isPlayable(playCard))
						return playCard;
			}
			
		return null;
	}

}
